package com.jjh.service.impl;

import com.fasterxml.jackson.databind.util.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jjh.consts.Result;
import com.jjh.controller.FileController;
import com.jjh.controller.TaoCanController;
import com.jjh.mapper.CategoryMapper;
import com.jjh.mapper.TaocanDishMapper;
import com.jjh.mapper.TaocanMapper;
import com.jjh.pojo.Category;
import com.jjh.pojo.Dish;
import com.jjh.pojo.Taocan;
import com.jjh.pojo.ref.TaocanDish;
import com.jjh.pojo.vo.TaocanVo;
import com.jjh.service.TaoCanService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class TaoCanServiceImpl implements TaoCanService {


    @Resource
    private TaocanMapper taocanMapper;
    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private TaocanDishMapper taocanDishMapper;


    @Value("${img.taocanImgPath}")
    private String taocanImgPath;



    @Override
    public Result page(Integer page, Integer size, Map<String, Object> map) {
        PageHelper.startPage(page,size);
        List<Taocan> taocans = getByCondition(map);
        PageInfo<Taocan> pageInfo = new PageInfo<>(taocans);
        return Result.ok(pageInfo);


    }

    /**
     * 多条件查询
     * @param map
     * @return
     */
    @Override
    public List<Taocan> getByCondition(Map<String, Object> map) {
        return taocanMapper.getByCondition(map);
    }

    /**
     * 添加套餐
     * @param taocanVo 套餐和菜品的组合数据
     * @return
     */
    @Override
    public Result addTaoCan(TaocanVo taocanVo) {
        //套餐校验
        String name = taocanVo.getName();
        if(ObjectUtils.isEmpty(name)){
            return Result.error("套餐名称不能为空!");
        }
        String category = taocanVo.getCategory();
        if(ObjectUtils.isEmpty(category)){
            return Result.error("套餐分类不能为空!");
        }
        String img = taocanVo.getImg();
        if(ObjectUtils.isEmpty(img)){
            return Result.error("套餐图片不能为空");
        }
        Double price = taocanVo.getPrice();
        if(ObjectUtils.isEmpty(price)||price<=0){
            return Result.error("套餐价格非法");
        }
        String status = taocanVo.getStatus();
        if(ObjectUtils.isEmpty(status) || (!"在售".equals(status)&&!"停售".equals(status))){
            return Result.error("套餐售卖状态非法!");
        }
        //菜品组合校验
//        List<Dish> dishList = taocanVo.getDishList();
//        for (Dish dish : dishList) {
//            if(dish.get)
//        }
        //然后是查询数据库看看套餐是否已经存在
        Taocan taocan = taocanMapper.getByName(name);
        if(!ObjectUtils.isEmpty(taocan)){
            return Result.error("该套餐已经存在！");
        }
        //查询套餐分类id
        Category category1 = categoryMapper.getByName(category);
        if(ObjectUtils.isEmpty(category1)){
            return Result.error("该套餐分类不存在！");
        }
        //都通过了，然后可以添加
        Taocan newTaocan =new Taocan();
        //属性复制
        BeanUtils.copyProperties(taocanVo,newTaocan);
        //设置分类id
        newTaocan.setCategoryId(category1.getId());
        //添加日期
        newTaocan.setCreateTime(LocalDateTime.now());
        newTaocan.setUpdateTime(LocalDateTime.now());
        //添加套餐
        log.info("套餐:{}",newTaocan);
        taocanMapper.insert(newTaocan);
        //然后是添加菜品组合
        List<TaocanDish> dishList = taocanVo.getDishList();
        //先给每一个套餐菜品实体赋值上套餐id
        dishList.forEach(it->it.setTaocanId(newTaocan.getId()));
        //批量添加
        log.info("菜品:{}",dishList);
         taocanDishMapper.addBatch(dishList);
        return Result.ok("添加成功!");
    }

    @Override
    public Result getById(Integer id) {
        Taocan taocan = taocanMapper.getById(id);
        if(ObjectUtils.isEmpty(taocan)){
            return Result.error("没有该套餐!");
        }else {
            return Result.ok(taocan);
        }
    }

    @Override
    public Result getTaoCanDishesByTid(Integer taocanId) {
       List<TaocanDish> taocanDishList =  taocanDishMapper.getByTaocanid(taocanId);
       if(ObjectUtils.isEmpty(taocanDishList)){
           return Result.error("为空！");
       }else {
           return Result.ok(taocanDishList);
       }
    }

    @Override
    public Result updateTaocan(TaocanVo taocanVo) {
        Integer id = taocanVo.getId();
        if(ObjectUtils.isEmpty(id)){
            return Result.error("套餐id不能为空!");
        }
        //套餐校验
        String name = taocanVo.getName();
        if(ObjectUtils.isEmpty(name)){
            return Result.error("套餐名称不能为空!");
        }
        String category = taocanVo.getCategory();
        if(ObjectUtils.isEmpty(category)){
            return Result.error("套餐分类不能为空!");
        }
        String img = taocanVo.getImg();
        if(ObjectUtils.isEmpty(img)){
            return Result.error("套餐图片不能为空");
        }
        Double price = taocanVo.getPrice();
        if(ObjectUtils.isEmpty(price)||price<=0){
            return Result.error("套餐价格非法");
        }
        String status = taocanVo.getStatus();
        if(ObjectUtils.isEmpty(status) || (!"在售".equals(status)&&!"停售".equals(status))){
            return Result.error("套餐售卖状态非法!");
        }
        //菜品组合校验
//        List<Dish> dishList = taocanVo.getDishList();
//        for (Dish dish : dishList) {
//            if(dish.get)
//        }
        //然后是查询数据库看看套餐是否是其他的套餐
        Taocan taocan = taocanMapper.getByName(name);
        if(!ObjectUtils.isEmpty(taocan) && !id.equals(taocan.getId())){
            return Result.error("该套餐已经存在而且是其他套餐，不能修改！");
        }
        //查询套餐分类id
        Category category1 = categoryMapper.getByName(category);
        if(ObjectUtils.isEmpty(category1)){
            return Result.error("该套餐分类不存在！");
        }
        //都通过了，然后可以修改
        Taocan newTaocan =new Taocan();
        //属性复制
        BeanUtils.copyProperties(taocanVo,newTaocan);
        //设置分类id
        newTaocan.setCategoryId(category1.getId());
        //添加日期
        newTaocan.setUpdateTime(LocalDateTime.now());
        //修改套餐
        taocanMapper.updateByPrimaryKey(newTaocan);

        //先把原来的菜品组合删除了
        taocanDishMapper.deleteByTid(id);
        //然后是添加菜品组合
        List<TaocanDish> dishList = taocanVo.getDishList();
        //先给每一个套餐菜品实体赋值上套餐id
        dishList.forEach(it->it.setTaocanId(newTaocan.getId()));
        //批量添加
        taocanDishMapper.addBatch(dishList);
        return Result.ok("修改成功!");
    }

    /**
     * 修改套餐售卖状态
     * @param id
     * @param status
     * @return
     */
    @Override
    public Result changeStatus(Integer id, Integer status) {
        if(status!=0&&status!=1) {
            return Result.error("状态非法");
        }
        int row = taocanMapper.updateStatus(id,status==1?"在售":"停售");
        if(row>0){
            return Result.ok("变更状态成功!");
        }else {
            log.error("修改套餐状态失败！{}","id:"+id+"status"+status);
            return Result.error("变更状态成功!");
        }
    }

    /**
     * 批量删除套餐
     * @param ids
     * @return
     */
    @Override
    public Result deleteTaocans(List<Integer> ids) {
        if(ObjectUtils.isEmpty(ids)){
            return Result.error("ids为空!");
        }
        //先查询出来套餐为了删除对应的图片
        for (Integer id : ids) {
            Taocan taocan = taocanMapper.getById(id);
            String img = taocan.getImg();
            //先删除套餐信息
            taocanMapper.deleteByPrimaryKey(id);
            //然后删除所有的菜品组合信息
            taocanDishMapper.deleteByTid(id);
            //最后删除套餐图片
            FileController.deleteImg(img,taocanImgPath);
        }
        return Result.ok("删除成功!");
    }
}
